package com.plm.redis.crud;

import com.plm.redis.utils.RedisUtil;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Limi
 * @create 2019-01-03 23:48
 */
public class TestJedisAPI {

    public static Jedis jedis = null;

    @BeforeClass
    public static void beforeClass(){
        jedis = RedisUtil.getJedis();
    }

    /**
     *字符串类型（string）
     */
    @Test
    public void testString(){
        // set/get
//        jedis.set("k1", "v1");
//        jedis.set("k2", "v2");
//        jedis.set("k3", "v3");
//        System.out.println(jedis.get("k1"));
//        System.out.println(jedis.get("k2"));
//        System.out.println(jedis.get("k3"));

        // mset/mget
//        jedis.mset("k4", "2", "k5", "v5");
//        System.out.println(jedis.mget("k1", "k2", "k3", "k4", "k5"));

        //incr/decr/incrby/decrby
//        jedis.incr("k4");
//        jedis.incrBy("k4", 20);
//        jedis.decrBy("k4", 40);
//        jedis.decr("k4");
//        System.out.println(jedis.get("k4"));

        //mestnx :当且仅当所有给定 key 都不存在时有效
//        jedis.msetnx("k8", "v8", "k9", "v9", "k10", "v10");
//        System.out.println(jedis.mget("k8", "k9", "k10"));

//        jedis.append("k1", "111111111111111111");
//        System.out.println(jedis.strlen("k1") + ",  " + jedis.get("k1"));

        //expire设置过期时间
//        jedis.expire("k1", 10);
//        Long t1 = jedis.ttl("t1");
//        System.out.println(t1);

        //getrange/setrange(从指定位置开始替换)
//        String getrange = jedis.getrange("k1", 0, -1);
//        System.out.println(getrange);
//        Long k1 = jedis.setrange("k1", 0, "11111");
//        System.out.println(k1 + ", " + jedis.get("k1"));

        //getset(先get再set)
//        String set = jedis.getSet("k1", "aaa");
//        System.out.println(set);
//        System.out.println(jedis.get("k1"));

    }

    /**
     * 列表List
     */
    @Test
    public void testList(){
        /**lpush/rpush/lrange**/
//        jedis.lpush("list1", "a", "b", "c", "d");
//        jedis.rpush("list2", "a", "b", "c", "d");
//        System.out.println(jedis.lrange("list1", 0, -1));//[d c b a]
//        System.out.println(jedis.lrange("list2", 0, -1));//[a b c d]

        /**lpop/rpop**/
//        String lpop = jedis.lpop("list1");
//        String rpop = jedis.rpop("list1");
//        System.out.println(lpop);//d
//        System.out.println(rpop);//a

        /**
         * lindex:获取指定下标的值
         * llen:获取列表里面的元素个数
         */
//        String e = jedis.lindex("list1", 0);
//        System.out.println(e);//c
//        System.out.println(jedis.llen("list1"));

        /**
         * lrem key 删N个value
         * ltrim 开始index 结束index，开始index 结束index，截取指定范围的值后再赋值给key
         * rpoplpush 源列表 目的列表
         * lset key index value,设置值
         * linsert key  before/after 值1 值2
         */
//        jedis.lpush("list3", "1", "1", "1","2", "2", "2","3", "3", "3");
//        jedis.lrem("list3", 3 ,"1");//删除3个1
//        jedis.ltrim("list3", 0, 2);//截取下标为0到2的值赋给list3
//        System.out.println(jedis.lrange("list3", 0, -1));
//        jedis.rpoplpush("list3", "list2");
//        System.out.println(jedis.lrange("list2", 0, -1)); //[3, a, b, c, d]
//        jedis.lset("list2", 0, "$");
//        System.out.println(jedis.lrange("list2", 0, -1));//[$, a, b, c, d]
        jedis.linsert("list2", ListPosition.BEFORE, "$", "￥");//在值$之前插入值￥
        System.out.println(jedis.lrange("list2", 0, -1));//[￥, $, a, b, c, d]

    }

    /**
     * 集合(set)
     */
    @Test
    public void testSet(){
        /**
         * sadd/smembers/sismember
         */
//        jedis.sadd("set1", "a", "a", "b", "b", "c", "c");
//        System.out.println(jedis.smembers("set1"));
//        System.out.println(jedis.sismember("set1", "a"));//测试值是否存在，true & false

        /**
         * scard，获取集合里面的元素个数
         */
//        System.out.println(jedis.scard("set1"));

        /**
         *  srem key value 删除集合中元素
         */
//        jedis.srem("set1", "a", "b");
//        System.out.println(jedis.smembers("set1"));

        /**
         * srandmember key 某个整数(随机出几个数)
         */
//        jedis.sadd("set2", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
//        System.out.println(jedis.srandmember("set2"));
//        System.out.println(jedis.srandmember("set2", 5));

        /**
         * spop key 随机出栈
         */
//        String spop = jedis.spop("set2");
//        System.out.println(spop);
//        System.out.println(jedis.smembers("set2"));

        /**
         * smove key1 key2 在key1里某个值, 作用是将key1里的某个值赋给key2
         */
//        jedis.smove("set2", "set1", "9");
//        System.out.println(jedis.smembers("set1"));
//        System.out.println(jedis.smembers("set2"));

        /**
         * 数学集合类:
         *  差集：sdiff
         *  交集：sinter
         *  并集：sunion
         */
        jedis.sadd("set3", "1", "2", "a", "b");
        jedis.sadd("set4", "1", "2", "3", "4");
        System.out.println(jedis.sdiff("set3", "set4"));//a,b
        System.out.println(jedis.sinter("set3", "set4"));//1,2
        System.out.println(jedis.sunion("set3", "set4"));//1,2,3,4,a,b
    }

    /**
     * 哈希（hash）,相当于java里的map
     * KV模式不变，但V是一个键值对
     */
    @Test
    public void testHash(){
        /**
         * hset/hget/hmset/hmget/hgetall/hdel
         */
//        jedis.hset("user", "id","001");
//        Map<String, String> hash = new HashMap<>();
//        hash.put("name", "zhangsan");
//        hash.put("age", "11");
//        hash.put("gender", "1");
//        jedis.hmset("user", hash);
//        String id = jedis.hget("user", "id");
//        System.out.println(id);
//        System.out.println(jedis.hgetAll("user"));

        /**
         * hlen
         * hexists key 在key里面的某个值的key
         * hkeys/hvals
         * hincrby/hincrbyfloat
         * hsetnx
         */
//        System.out.println(jedis.hlen("user"));
//        System.out.println(jedis.hexists("user", "hobby"));
//        Set<String> keys = jedis.hkeys("user");
//        List<String> vals = jedis.hvals("user");
//        keys.forEach(System.out::println);
//        vals.forEach(System.out::println);
//        jedis.hincrBy("user", "age", 10);
//        System.out.println(jedis.hget("user", "age"));
        jedis.hsetnx("user","hobby", "basketball");
        System.out.println(jedis.hgetAll("user"));
    }

    /**
     * 有序集合Zset(sorted set)
     * 在set基础上，加一个score值。
     * 之前set是k1 v1 v2 v3，
     * 现在zset是k1 score1 v1 score2 v2
     */
    @Test
    public void testZset(){
        /**
         * zadd/zrange (Withscores)
         * zrangebyscore key 开始score 结束score
         * zrem key 某score下对应的value值，作用是删除元素
         * zcard/zcount key score区间/zrank key values值，作用是获得下标值/zscore key 对应值,获得分数
         * zrevrank key values值，作用是逆序获得下标值
         * zrevrange
         * zrevrangebyscore  key 结束score 开始score
         */
//        jedis.zadd("zset1", 60.0, "v1");
//        Map<String, Double> scoreMembers = new HashMap<>();
//        scoreMembers.put("v2", 70.0);
//        scoreMembers.put("v3", 80.0);
//        scoreMembers.put("v4", 90.0);
//        scoreMembers.put("v5", 100.0);
//        jedis.zadd("zset1", scoreMembers);
//        System.out.println(jedis.zrangeWithScores("zset1", 0, -1));

//        Set<String> zset1 = jedis.zrangeByScore("zset1", 60, 90);
//        System.out.println(zset1);

//        jedis.zrem("zset1", "v1");
//        System.out.println(jedis.zrange("zset1", 0, -1));

//        System.out.println(jedis.zcard("zset1"));
//        System.out.println(jedis.zcount("zset1", 80, 100));

//        Long zrevrank = jedis.zrevrank("zset1", "v3");//作用是逆序获得下标值
//        System.out.println(zrevrank);

//        Set<String> zset1 = jedis.zrevrange("zset1", 0, -1);
//        System.out.println(zset1);

        Set<String> zset1 = jedis.zrevrangeByScore("zset1", 100, 60);
        System.out.println(zset1);
    }

}
